package com.paic.mhis.hcpms.common.sso.biz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.Authority;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.common.sso.biz.service.CognosReportService;
import com.paic.mhis.hcpms.common.sso.dto.CognosReportDTO;
import com.paic.mhis.hcpms.common.sso.dto.CognosRoleDTO;
import com.paic.mhis.hcpms.common.sso.intergration.dao.CognosReportDAO;
import com.paic.mhis.hcpms.common.sso.util.CognosConstants;
import com.paic.pafa.exceptions.BusinessException;

@Service("cognosReportServiceImpl")
public class CognosReportServiceImpl implements CognosReportService {

	@Autowired
	private CognosReportDAO cognosReportDao;

	@Override
	public Map<String, Object> loadCognosReportInfo(UserDetails user, String reportName, int startNum, int endNum,
			Map<String, Object> map) {

		Map<String, Object> returnMap = new HashMap<String, Object>();

		Map<String, Object> parmaterMap = new HashMap<String, Object>();

		List<Authority> authorList = user.getAuthorities();
		StringBuffer sb = new StringBuffer();
		String role = "";
		for (Authority authority : authorList) {
			sb = sb.append("'" + authority.getAuthority() + "',");
		}
		if (sb != null) {
			role = sb.substring(0, sb.length() - 1);
		}
		parmaterMap.put("roleName", role);
		if (StringUtils.hasText(reportName)) {
			parmaterMap.put("reportName", "'%" + reportName + "%'");
		}

		parmaterMap.put("startNum", startNum);
		parmaterMap.put("endNum", endNum);

		parmaterMap.putAll(map);

		List<CognosReportDTO> list = this.cognosReportDao.queryCognosReportDTO(parmaterMap);
		long count = this.cognosReportDao.queryCognosReportCount(parmaterMap);

		returnMap.put("data", list);
		returnMap.put("count", count);
		return returnMap;
	}

	@Override
	public List<CognosReportDTO> findCognosReports(Map<String, Object> paramMap) throws BusinessException {
		return cognosReportDao.findCognosReports(paramMap);
	}

	@Transactional
	@Override
	public Map<String, Object> processCognosReportInfo(UserDetails user, String processType, CognosReportDTO bean) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		if (CognosConstants.process_save.equals(processType)) { // 保存
			this.cognosReportDao.saveCognosReportInfo(bean);
		}

		if (CognosConstants.process_update.equals(processType)) { // 更新
			bean.setUpdateUser(user.getUsername());
			this.cognosReportDao.updateCognosReportInfo(bean);

		}
		returnMap.put("flag", true);
		returnMap.put("msg", "success");
		return returnMap;
	}

	@Override
	public Map<String, Object> loadSignleCognosReportInfo(UserDetails user, String reportName, int startNum,
			int endNum, Map<String, Object> map) {

		Map<String, Object> returnMap = new HashMap<String, Object>();

		Map<String, Object> parmaterMap = new HashMap<String, Object>();
		if (StringUtils.hasText(reportName)) {
			parmaterMap.put("reportName", "'%" + reportName + "%'");
		}

		parmaterMap.put("startNum", startNum);
		parmaterMap.put("endNum", endNum);

		parmaterMap.putAll(map);

		List<CognosReportDTO> list = this.cognosReportDao.loadSigleCognosReportDTOInfo(parmaterMap);
		long count = this.cognosReportDao.countSigleCognosReportDTOInfo(parmaterMap);

		returnMap.put("data", list);
		returnMap.put("count", count);
		return returnMap;
	}

	@Transactional
	@Override
	public void batchDeleteCognosReportInfo(String[] reportListId) {

		for (String id : reportListId) {
			CognosReportDTO bean = new CognosReportDTO();
			bean.setId(id);
			this.cognosReportDao.deleteCognosReportInfo(bean);

		}

	}

	@Transactional
	@Override
	public boolean saveCognosReportAuthorize(JSONObject transferJson, String userName, Map<String, Object> paramMap)
			throws BusinessException {
		cognosReportDao.deleteCognosReportRole(paramMap);
		List<CognosRoleDTO> cognosRoles = new ArrayList<CognosRoleDTO>();
		JSONArray jsonArr = (JSONArray) transferJson.remove(CognosConstants.ROLE_REPORTS);
		if (jsonArr == null) {
			return false;
		}
		if (jsonArr.size() > 0) {
			for (int j = 0; j < jsonArr.size(); j++) {
				CognosRoleDTO dto = new CognosRoleDTO();
				JSONObject tempJson = jsonArr.getJSONObject(j);
				dto = JSONObject.toJavaObject(tempJson, CognosRoleDTO.class);
				if (dto != null) {
					dto.setCreatedBy(userName);
					dto.setUpdatedBy(userName);
					cognosRoles.add(dto);
				}
			}
			cognosReportDao.saveCognosReportAuthorize(cognosRoles);
			return true;
		}
		return false;
	}

	@Override
	public List<CognosRoleDTO> cognosReportQueryRole(Map<String, Object> paramMap) throws BusinessException {
		return cognosReportDao.cognosReportQueryRole(paramMap);
	}

	@Override
	public List<CognosReportDTO> cognosReportAuthorizeQuery(Map<String, Object> paramMap) throws BusinessException {
		return cognosReportDao.cognosReportAuthorizeQuery(paramMap);
	}

	@Transactional
	@Override
	public void saveCognosReportAuthorizeInfo(String[] roleIds, String[] reportIds, UserDetails user) {

		if (null != roleIds && roleIds.length > 0 && reportIds == null) {

			for (String roleId : roleIds) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("roleId", roleId);
				this.cognosReportDao.deleteCognosReportRole(map);
			}

		} else if (roleIds == null && reportIds != null && reportIds.length > 0) {

			for (String reportId : reportIds) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("reportUrlId", reportId);
				this.cognosReportDao.deleteCognosReportRole(map);
			}
		} else if (null != roleIds && roleIds.length > 0 && null != reportIds && reportIds.length > 0) {
			List<CognosRoleDTO> cognosRolesList = new ArrayList<CognosRoleDTO>();
			for (String roleId : roleIds) {

				Map<String, Object> map = new HashMap<String, Object>();
				map.put("roleId", roleId);
				this.cognosReportDao.deleteCognosReportRole(map);

				for (String reportId : reportIds) {
					CognosRoleDTO bean = new CognosRoleDTO();
					bean.setReportUrlId(reportId);
					bean.setRoleId(roleId);
					bean.setCreatedBy(user.getUsername());
					bean.setUpdatedBy(user.getUsername());

					cognosRolesList.add(bean);
				}
			}

			this.cognosReportDao.saveCognosReportAuthorize(cognosRolesList);

		} else {
			Map<String, Object> map = new HashMap<String, Object>();
			this.cognosReportDao.deleteCognosReportRole(map);
		}

	}

	@Override
	public List<CognosReportDTO> loadReportByRoleId(String roleId) {

		List<CognosReportDTO> list = this.cognosReportDao.queryCognosReportDTOByRoleId(roleId);
		return list;
	}

	@Override
	public List<CognosRoleDTO> loadRoleInfo() {

		return this.cognosReportDao.queryAllRoleInfo();
	}
}
